package com.ruoyi.activiti.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.activiti.constanst.ProcessType;
import com.ruoyi.activiti.domain.BizExpensesClaim;
import com.ruoyi.activiti.domain.BizLeave;
import com.ruoyi.activiti.domain.CheckTask;
import com.ruoyi.activiti.domain.InstanceBusiness;
import com.ruoyi.activiti.mapper.BizExpensesClaimMapper;
import com.ruoyi.activiti.mapper.BizLeaveMapper;
import com.ruoyi.activiti.mapper.CheckTaskMapper;
import com.ruoyi.activiti.mapper.TaskMapper;
import com.ruoyi.activiti.service.IBizExpensesService;
import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class BizExpenseServiceImpl implements IBizExpensesService {
    @Autowired
    private BizExpensesClaimMapper bizExpensesClaimMapper;
    @Autowired
    private IProcessService processService;
    @Autowired
    private CheckTaskMapper checkTaskMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Override
    public BizExpensesClaim selectBizLeaveById(Long id) {
        return bizExpensesClaimMapper.selectBizExpensesById(id);
    }

    @Override
    public BizExpensesClaim selectBizLeaveByInstanceId(String instanceId) {
        return bizExpensesClaimMapper.selectBizExpensesByInstanceId(instanceId);
    }

    @Override
    public List<BizExpensesClaim> selectBizExpensesList(BizExpensesClaim bizExpensesClaim) {
        return bizExpensesClaimMapper.selectBizExpensesList(bizExpensesClaim);
    }

    @Override
    public int insertBizLeave(BizExpensesClaim bizLeave) {
        return bizExpensesClaimMapper.insertBizExpenses(bizLeave);
    }

    @Override
    public int updateBizExpensesById(BizExpensesClaim bizExpensesClaim) {
        return bizExpensesClaimMapper.updateBizExpensesById(bizExpensesClaim);
    }

    public void submitApply(Long id) throws Exception {
        BizExpensesClaim bizLeave = bizExpensesClaimMapper.selectBizExpensesById(id);
        Map<String,Object> data = new HashMap<>();
        data.put("name",bizLeave.getItemName());
        data.put("type", "报销");
        Map<String,Object> vars = new HashMap<>();
        vars.put("data",data);
        String instanceId = processService.submitApply("expenses",vars);
        String username = SecurityUtils.getUsername();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        Date now = new Date();
        bizLeave.setInstanceId(instanceId);
        bizLeave.setApplyName(nickName);
        bizLeave.setApplyTime(now);
        bizLeave.setProcessKey("expenses");
        bizExpensesClaimMapper.updateBizExpensesById(bizLeave);
        InstanceBusiness ib = new InstanceBusiness();
        ib.setInstanceId(instanceId);
        ib.setBusinessKey(id + "");
        ib.setModule(humpToLine(bizLeave.getClass().getSimpleName()).substring(1));
        taskMapper.insertInstanceBusiness(ib);
    }
    private String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    @Override
    public void checkTask(CheckTask checkTask) {
        String userName = SecurityUtils.getUsername();
        checkTask.setCreatedBy(userName);
        checkTask.setCreatedTime(LocalDateTime.now());
        checkTaskMapper.insert(checkTask);
        Map<String,String> map = new HashMap<>();
        map.put("pass",checkTask.getReviewStatus());
        map.put("comment",checkTask.getReason());
        String vars = JSON.toJSONString(map);
        processService.complete(checkTask.getTaskId(),checkTask.getInstanceId(),vars);
    }

    @Override
    public CheckTask selectByInstanceId(CheckTask checkTask) {
        return checkTaskMapper.queryByInstanceId(checkTask);
    }
}
